<!DOCTYPE html>
<!--
Copyright 2017 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/value/diagnostics/breakdown.html">
<link rel="import" href="/tracing/value/histogram_deserializer.html">
<link rel="import" href="/tracing/value/histogram_serializer.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  test('merge', function() {
    const a = new tr.v.d.Breakdown();
    a.set('x', 1);
    a.set('y', 2);

    const b = new tr.v.d.Breakdown();
    b.set('y', 3);
    b.set('z', 4);

    assert.isTrue(a.canAddDiagnostic(b));
    assert.isTrue(b.canAddDiagnostic(a));

    a.addDiagnostic(b);
    assert.strictEqual(a.get('x'), 1);
    assert.strictEqual(a.get('y'), 5);
    assert.strictEqual(a.get('z'), 4);

    a.colorScheme = 'fake color scheme';
    assert.isFalse(a.canAddDiagnostic(b));
    assert.isFalse(b.canAddDiagnostic(a));
  });

  test('deserialize', function() {
    const b = tr.v.d.Breakdown.deserialize(
        [4, 3, 1, 2, 3],
        new tr.v.HistogramDeserializer([
          'a', 'b', 'c',
          [0, 1, 2],
          'colors',
        ]));
    assert.strictEqual(b.colorScheme, 'colors');
    assert.strictEqual(b.get('a'), 1);
    assert.strictEqual(b.get('b'), 2);
    assert.strictEqual(b.get('c'), 3);
  });

  test('serialize', function() {
    const s = new tr.v.HistogramSerializer();
    const b = tr.v.d.Breakdown.fromEntries([['a', 10], ['b', 20]]);
    assert.deepEqual(b.serialize(s), [0, 3, 10, 20]);
    assert.strictEqual(s.getOrAllocateId(''), 0);
    assert.strictEqual(s.getOrAllocateId('a'), 1);
    assert.strictEqual(s.getOrAllocateId('b'), 2);
    assert.strictEqual(s.getOrAllocateId([1, 2]), 3);
  });
});
</script>
